﻿///////////////////////////////////////////////////////////////////////////////////
/// 文 件 名: MaterialsProcurements.cs
/// 作    者: XXD
/// 创建日期: 2012/6/19 8:58:15
/// 
///---------------------------------修改记录--------------------------------------
/// 修 改 人:          
/// 修改日期:
/// 修改原因: 
//////////////////////////////////////////////////////////////////////////////////
namespace ZX.BPM.Library
{
    using System;
    using System.Data.SqlClient;
    using System.Linq;
    using Csla;
    using Csla.Data;
    using ZX.DAL;

    [Serializable()]
    public class MaterialsProcurements : BusinessListBase<MaterialsProcurements, MaterialsProcurement>
    {
        public void Update(MaterialsProcurement materialsProcurement)
        {
            foreach (MaterialsProcurement p in this)
            {
                if (p.ID == materialsProcurement.ID)
                {
                    p.Quantity = materialsProcurement.Quantity;
                    p.PricesExpected = materialsProcurement.PricesExpected;
                    p.Goods = materialsProcurement.Goods;
                    p.Explain = materialsProcurement.Explain;
                    p.Specification = materialsProcurement.Specification;
                    break;
                }
            }

        }
        public void Remove(Guid procurementID)
        {
            var item = (from r in this
                        where r.ID == procurementID
                        select r).FirstOrDefault();
            if (item != null)
            {
                Remove(item);
            }


        }
        public void Add(MaterialsProcurement materialsProcurement)
        {
            if (!(Contains(materialsProcurement.ID)))
            {
                MaterialsProcurement p = MaterialsProcurement.NewMaterialsProcurement();
                p.Quantity = materialsProcurement.Quantity;
                p.PricesExpected = materialsProcurement.PricesExpected;
                p.Goods = materialsProcurement.Goods;
                p.Explain = materialsProcurement.Explain;
                p.Specification = materialsProcurement.Specification;
                p.DemandID = materialsProcurement.DemandID;
              //  p.ID = Guid.NewGuid();
                base.Add(p);
            }
            else
            {
                throw new InvalidOperationException("Resource already assigned to project");
            }
        }

        public bool Contains(Guid procurementID)
        {
            var item = (from r in this
                        where r.ID == procurementID
                        select r).Count();
            return item > 0;
        }

        public bool ContainsDeleted(Guid procurementID)
        {
            var item = (from r in DeletedList
                        where r.ID == procurementID
                        select r).Count();
            return item > 0;
        }

        private void Child_Fetch(Guid demandID)
        {
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames.OpenExpressApp))
            {
                DAL.DBExtend.DbSql db = new DAL.DBExtend.DbSql();
                string strSql = string.Format("SELECT ID FROM dbo.bpm_MaterialsProcurement WHERE DemandID='{0}'", demandID);
                System.Data.DataTable data = db.ExecuteQuery(strSql);

                var rlce = RaiseListChangedEvents;
                RaiseListChangedEvents = false;
                foreach (System.Data.DataRow item in data.Rows)
                    base.Add(DataPortal.FetchChild<MaterialsProcurement>(new Guid(item["ID"].ToString())));
                RaiseListChangedEvents = rlce;
            }
        }

        #region  Factory Methods

        internal static MaterialsProcurements NewMaterialsProcurements()
        {
            return DataPortal.CreateChild<MaterialsProcurements>();
        }


        public MaterialsProcurements()
        { /* require use of factory methods */ }

        #endregion
    }
}
